package com.im.app.controller;


import com.im.app.authConfig.CurrentUser;
import com.im.app.authConfig.RequireLogin;
import com.im.app.model.Admin;
import com.im.app.model.Kuaijie;
import com.im.app.model.User;
import com.im.app.service.KuaijieService;
import com.im.app.utils.Response;
import com.im.app.utils.ResponseResult;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "/fast")
public class KuaijieController extends BaseController{

    @Resource
    private KuaijieService kuaijieService;

    @RequireLogin
    @GetMapping(value = "/list")
    public ResponseResult<Object> queryKuaijieList(@CurrentUser User user) {
        List<Kuaijie> kuaijieList = kuaijieService.queryKuaijieList(user.getUserId());
        return Response.makeOKRsp().setData(kuaijieList);
    }

    @RequireLogin
    @GetMapping(value ="/find")
    public ResponseResult<Object> findKuaijie(Integer id) {
        Kuaijie kuaijie = kuaijieService.findKuaijie(id);
        return Response.makeOKRsp().setData(kuaijie);

    }
    @RequireLogin
    @GetMapping(value ="/delete")
    public ResponseResult<Object> deleteKuaijie(Integer id) {
        int result = kuaijieService.deleteKuaijie(id);
        if(result>0){
            return Response.makeOKRsp("删除成功");
        }else{
            return Response.makeErrRsp("删除失败");
        }
    }

    @RequireLogin
    @PostMapping(value ="/add")
    public ResponseResult<Object> addKuaijie(@CurrentUser User user, @RequestBody Kuaijie kuaijie) {
        kuaijie.setUserId(user.getUserId());
        int result = kuaijieService.addKuaijie(kuaijie);
        if(result>0){
            return Response.makeOKRsp("添加成功");
        }else{
            return Response.makeErrRsp("添加失败");
        }
    }

    @RequireLogin
    @PostMapping(value ="/update")
    public ResponseResult<Object> updateKuaijie(@CurrentUser User user, @RequestBody Kuaijie kuaijie) {
        kuaijie.setUserId(user.getUserId());
        int result = kuaijieService.updateKuaijie(kuaijie);
        if(result>0){
            return Response.makeOKRsp("修改成功");
        }else{
            return Response.makeErrRsp("修改失败");
        }
    }


    @RequireLogin
    @PostMapping(value ="/admin/add")
    public ResponseResult<Object> adminAdd(@RequestHeader String token, @RequestBody Kuaijie kuaijie) {
        Admin admin = getAdmin(token);
        if (admin == null){
            return Response.makeRsp(401,"登录已过期");
        }
        kuaijie.setUserId(admin.getId()+"");
        int result = kuaijieService.addKuaijie(kuaijie);
        if(result>0){
            return Response.makeOKRsp("添加成功");
        }else{
            return Response.makeErrRsp("添加失败");
        }
    }

    @RequireLogin
    @GetMapping(value = "/admin/list")
    public ResponseResult<Object> getAdminList(@RequestHeader String token) {
        Admin admin = getAdmin(token);
        if (admin == null){
            return Response.makeRsp(401,"登录已过期");
        }
        List<Kuaijie> kuaijieList = kuaijieService.queryKuaijieList(admin.getId()+"");
        return Response.makeOKRsp().setData(kuaijieList);
    }

    @RequireLogin
    @PostMapping(value ="/admin/update")
    public ResponseResult<Object> adminUpdate(@RequestHeader String token, @RequestBody Kuaijie kuaijie) {
        Admin admin = getAdmin(token);
        if (admin == null){
            return Response.makeRsp(401,"登录已过期");
        }
        kuaijie.setUserId(admin.getId() + "");
        int result = kuaijieService.updateKuaijie(kuaijie);
        if(result>0){
            return Response.makeOKRsp("修改成功");
        }else{
            return Response.makeErrRsp("修改失败");
        }
    }

    @RequireLogin
    @GetMapping(value ="/admin/delete")
    public ResponseResult<Object> adminDeleted(Integer id) {
        int result = kuaijieService.deleteKuaijie(id);
        if(result>0){
            return Response.makeOKRsp("删除成功");
        }else{
            return Response.makeErrRsp("删除失败");
        }
    }
}
